<?php
class Blog_PostController extends Areal_Controller_Action
{
    public function indexAction()
    {        
        $postGateway = new Blog_Model_PostGateway();
        $currentPage = $this->_getParam('page');
        $pager = $postGateway->getPager($currentPage);
        $this->view->assign('posts', Areal_Paginator::build($pager));        
    }

    public function adminAction()
    {
        $postGateway = new Blog_Model_PostGateway();
        $currentPage = $this->_getParam('page');
        $pager = $postGateway->getAdminPager($currentPage);
        $this->view->assign('posts', Areal_Paginator::build($pager));
    }

    public function addAction()
    {
        $postGateway = new Blog_Model_PostGateway();
        if ($this->getRequest()->isPost()){
            if ($postGateway->add($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('post saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('post not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                $this->view->form = $postGateway->getForm();
            }
        } else{
            $this->view->form = $postGateway->getForm();
        }
    }

    public function editAction()
    {
        $post_id = $this->getRequest()->getParam('id');
        $postGateway = new Blog_Model_PostGateway();
        if ($this->getRequest()->isPost()){
            if ($postGateway->edit($this->getRequest()->getPost(), $post_id)){
                $message = Zend_Registry::get('Zend_Translate')->translate('post saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('post not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                $this->view->form = $postGateway->getForm();
            }
        } else {
            $this->view->form = $postGateway->getForm($post_id);
        }
    }

    public function showAction()
    {
        // post with its comments
        $post_id = $this->getRequest()->getParam('post_id');
        $postGateway = new Blog_PostGateway();
        $post_data = $postGateway->getPostData($post_id);
        $this->view->assign('post', $post_data);

        // comment form
        $commentGateway = new Blog_CommentGateway();
        if ($this->getRequest()->isPost()){
            if ($commentGateway->add($this->getRequest()->getPost(), $post_id)){
                $message = Zend_Registry::get('Zend_Translate')->translate('comment saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('show','post','blog',array('post_id'=>$post_id));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('comment not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->commentForm = $commentGateway->getForm($post_id);
    }

    public function deleteAction(){
        $posted_data = $this->getRequest()->getParam('group_action');

        // if group action posted and item selected
        if( isset($posted_data['selected_items']) && count($posted_data['selected_items']) ){
            $this->getHelper('flashMessenger')->addMessage('executing delete');
            $postGateway = new Blog_PostGateway();
            $postGateway->deletePosts($posted_data['selected_items']);
            return $this->getHelper('Redirector')->goto('admin');
        } else {
            $message = $this->getHelper('Translator')->translate('no item were selected');
            $this->getHelper('flashMessenger')->addMessage($message);
            $this->getRequest()->setDispatched(false);
            $this->getRequest()->setActionName('admin');
        }
    }   
}
